package com.brand.prisd.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.brand.prisd.R;
import com.brand.prisd.fragment.HomeFragment;
import com.brand.prisd.fragment.MeFragment;
import com.brand.prisd.fragment.SmartFragment;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, TabLayout.OnTabSelectedListener {

    @BindView(R.id.tl)
    TabLayout tl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        showFragment(0);
    }

    private void initView() {
        tl = findViewById(R.id.tl);
        tl.addTab(tl.newTab().setText(R.string.home_page));
        tl.addTab(tl.newTab().setText(R.string.smart));
        tl.addTab(tl.newTab().setText(R.string.me));

        tl.addOnTabSelectedListener(this);
    }


    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    int currentIndex = -1;
    private Fragment[] fragments = new Fragment[3];

    public void showFragment(int nextIndex) {

        if (currentIndex == nextIndex) {
            return;
        }
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (fragments[nextIndex] == null) {
            switch (nextIndex) {
                case 0:
                    fragments[nextIndex] = new HomeFragment();
                    break;
                case 1:
                    fragments[nextIndex] = new SmartFragment();
                    break;
                case 2:
                    fragments[nextIndex] = new MeFragment();
                    break;
            }
            ft.add(R.id.fl_container, fragments[nextIndex]);
        } else {
            ft.show(fragments[nextIndex]);
        }
        if (currentIndex != -1) {
            ft.hide(fragments[currentIndex]);
        }
        ft.commit();
        currentIndex = nextIndex;
    }

    long last = 0;

    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        long L = System.currentTimeMillis();
        if (L - last >= 3000) {
            Toast.makeText(this, "再按一次退出 PRISD", Toast.LENGTH_SHORT).show();
        } else {
            finish();
        }
        last = L;
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        showFragment(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    @Override
    public void onClick(View v) {

    }
}
